package com.nut.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestAttribute;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.time.LocalDate;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  @Bean
  public Docket apiAdmin() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("对外开放 API")
        .select()
        .apis(RequestHandlerSelectors.any())
        // 扫描所有有注解的api，用这种方式更灵活
        //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .paths(regex("/admin.*"))
        .build()
        //添加登录认证
        //.securitySchemes(this.securitySchemes())
        //.securityContexts(this.securityContexts())
        //
        .ignoredParameterTypes(RequestAttribute.class)
        .directModelSubstitute(LocalDate.class, String.class)
        .apiInfo(this.apiInfo())
        .enable(true);
  }

  @Bean
  public Docket apiMerchant() {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("PC机构端 API")
        .select()
        .apis(RequestHandlerSelectors.any())
        // 扫描所有有注解的api，用这种方式更灵活
        //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .paths(regex("/merchant.*"))
        .build()
        //添加登录认证
        //.securitySchemes(this.securitySchemes())
        //.securityContexts(this.securityContexts())
        //
        .ignoredParameterTypes(RequestAttribute.class)
        .directModelSubstitute(LocalDate.class, String.class)
        .apiInfo(this.apiInfo())
        .enable(true);
  }

  private ApiInfo apiInfo() {
    // 用ApiInfoBuilder进行定制
    return new ApiInfoBuilder()
            // 设置标题
            .title("媄众APP接口文档")
            // 描述
            .description("用于管理媄众APP对应API")
            .contact(new Contact("mingjun", "https://github.com/tc11o8", "284040429@qq.com"))
            .version("1.0.0")
            .build();
  }
}
